package com.koron.sinoprobe.web.engineering.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.web.engineering.bean.po.SpEstablish;
import com.koron.sinoprobe.web.engineering.bean.po.SpTechDisclosure;
import com.koron.sinoprobe.web.engineering.bean.query.SpTechDisclosureQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpTechDisclosureVO;
import com.koron.sinoprobe.web.engineering.mapper.SpTechDisclosureMapper;
import com.koron.sinoprobe.web.engineering.service.SpEstablishService;
import com.koron.sinoprobe.web.engineering.service.SpTechDisclosureService;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 技术交底(SpTechDisclosure)表服务实现类
 *
 * @author jzp
 * @since 2021-12-01 10:14:40
 */
@Service
public class SpTechDisclosureServiceImpl implements SpTechDisclosureService {

    @Autowired
    private SpEstablishService spEstablishService;

    @Autowired
    private SpProjectService spProjectService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpTechDisclosureVO getById(SessionFactory factory, String id) {
        SpTechDisclosureMapper mapper = factory.getMapper(SpTechDisclosureMapper.class);
        SpTechDisclosureVO spTechDisclosureVO = mapper.selectById(id);
        SpEstablish spEstablish = spEstablishService.getByProjectId(factory, spTechDisclosureVO.getProjectId());
        spTechDisclosureVO.setSpEstablish(spEstablish);
        SpProject spProject = spProjectService.getById(factory, spTechDisclosureVO.getProjectId());
        spTechDisclosureVO.setSpProject(spProject);
        return spTechDisclosureVO;
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpTechDisclosureVO> list(SessionFactory factory, SpTechDisclosureQuery query) {
        SpTechDisclosureMapper mapper = factory.getMapper(SpTechDisclosureMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpTechDisclosureVO> page(SessionFactory factory, PageQuery pageQuery, SpTechDisclosureQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpTechDisclosureVO> spTechDisclosureList = list(factory, query);
        return new PageInfo<>(spTechDisclosureList);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpTechDisclosure spTechDisclosure) {
        SpTechDisclosureMapper mapper = factory.getMapper(SpTechDisclosureMapper.class);
        return mapper.insertOrUpdate(spTechDisclosure) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpTechDisclosure spTechDisclosure) {
        SpTechDisclosureMapper mapper = factory.getMapper(SpTechDisclosureMapper.class);
        return mapper.update(spTechDisclosure) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpTechDisclosureMapper mapper = factory.getMapper(SpTechDisclosureMapper.class);
        return mapper.hidden(id) > 0;
    }
}
